﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using Meqs.Entity;

public partial class Student_Report_StuView : System.Web.UI.Page
{
    protected List<StuExam> stuExam = new List<StuExam>();
    protected List<int> schoolYear = new List<int>();
    /// <summary>
    /// 本次考试的科目(统考)
    /// </summary>
    protected IList<ExamSubjecyEducation2> subjEdu = null;
    /// <summary>
    /// 本次考试的科目(校考)
    /// </summary>
    protected IList<ExamSubjecySchool2> subjSchool = null;
    /// <summary>
    /// 统考成绩列表（已经输入的）
    /// </summary>
    protected IList<GradeScoreEducation> gse = null;
    /// <summary>
    /// 校考试成绩
    /// </summary>
    protected IList<ExamDetailSchool> eds = null;
    protected int examType;
    protected int examid;
    protected int grade;
    protected string examName;
    protected int classType;
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            Hashtable ht = null;
            for (int i = 1; i < 13;i++ )
            {
                ht = new Hashtable();
                ht.Add("StudentID", CommonScore.LogonStudent.RID);
                ht.Add("TableName",CommonScore.GetGradeStoreTable(i));
                IList<ExamsEducation> eus = Container<Meqs.Logic.Interface.IExamsEducationLogic>.Instance.FindStudentExam(ht);
                foreach(ExamsEducation u in eus)
                {
                    StuExam st = new StuExam(1,u.RID,u.Name,u.SchoolYear,u.Grade,u.ClassType);
                    stuExam.Add(st);
                }
            }

            ht = new Hashtable();
            ht.Add("StudentID", CommonScore.LogonStudent.ClsID);
            ht.Add("SchoolMark", CommonScore.GetSchoolMark);
            IList<ExamsSchool> ess = Container<Meqs.Logic.Interface.IExamsSchoolLogic>.Instance.FindStudentExam(ht);
            foreach (ExamsSchool u in ess)
            {
                StuExam st = new StuExam(0, u.RID, u.Name, u.SchoolYear, u.Grade, u.ClassType);
                stuExam.Add(st);
            }

            foreach (StuExam se in stuExam)
            {
                if (!schoolYear.Contains(se.schoolYear))
                {
                    schoolYear.Add(se.schoolYear);
                }
            }

        }
    }
    /// <summary>
    /// 获得某学生的统考分数
    /// </summary>
    /// <param name="stuId"></param>
    /// <param name="subjId"></param>
    /// <returns></returns>
    private string GetTongKaoScore(int stuId, int subjId)
    {

        foreach (GradeScoreEducation g in gse)
        {
            if (g.StudentID == stuId && g.SubjectID == subjId)
            {
                return (g.Score == null) ? "null" : g.Score.ToString();
            }
        }
        return "null";
    }
    /// <summary>
    /// 获得某学生的校考分数
    /// </summary>
    /// <param name="stuId"></param>
    /// <param name="subjId"></param>
    /// <returns></returns>
    private string GetXiaoKaoScore(int stuId, int subjId)
    {
        foreach (ExamDetailSchool e in eds)
        {
            if (e.StudentID == stuId && e.SubjectID == subjId)
            {
                return (e.Score == null) ? "null" : e.Score.ToString();
            }
        }
        return "null";
    }
    protected string GetStudent()
    {
        string header = "<tr  style='background-color: White;' >";

        int w = (800 - 160);
        header += "<td width='80px' style='text-align:left;padding-left: 5px;'>" + CommonScore.LogonStudent.StudentCode + "</td><td width='80px' style='text-align:left;padding-left: 5px;'>" + CommonScore.LogonStudent.StudentName + "</td>";
        if (examType == 1)
        {
            if (subjEdu != null)
            {
                w = (subjEdu.Count == 0 ? 0 : w / (1 + subjEdu.Count));
                double fullscore = 0;
                foreach (ExamSubjecyEducation2 ese in subjEdu)
                {
                    string score = GetTongKaoScore(CommonScore.LogonStudent.RID, ese.SubjectID);
                    fullscore += Convert.ToDouble((score == "null" ? "0" : score));
                    header += "<td width='" + w + "px' style='text-align:right;padding-right: 5px;'>" + (score == "null" ? "" : score) + "</td>";
                }
                if (subjEdu.Count >= 0)
                {
                    header += "<td width='" + w + "px' style='text-align:right;padding-right: 5px;'>" + fullscore + "</td>";
                }
            }
        }
        else
        {
            if (subjSchool != null)
            {
                w = (subjSchool.Count == 0 ? 0 : w / (1 + subjSchool.Count));
                double fullscore = 0;
                foreach (ExamSubjecySchool2 ese in subjSchool)
                {
                    string score = GetXiaoKaoScore(CommonScore.LogonStudent.RID, ese.SubjectID);
                    fullscore += Convert.ToDouble((score == "null" ? "0" : score));
                    header += "<td width='" + w + "px' style='text-align:right;padding-right: 5px;'>" + (score == "null" ? "" : score) + "</td>";
                }
                if (subjSchool.Count >= 0)
                {
                    header += "<td width='" + w + "px' style='text-align:right;padding-right: 5px;'>" + fullscore + "</td>";
                }
            }
        }


        header += "</tr>";
        return header;
    }
    protected string GetHeader()
    {
        int w = (800 - 160);
        string header = "<th width='80px' style='text-align:left;padding-left: 5px;'>学号</th><th width='80px' style='text-align:left;padding-left: 5px;'>姓名</th>";
        if (examType == 1)
        {
            if (subjEdu != null)
            {
                w = (subjEdu.Count == 0 ? 0 : w / (subjEdu.Count + 1));
                foreach (ExamSubjecyEducation2 ese in subjEdu)
                {
                    header += "<th width='" + w + "px' style='text-align:right;padding-right: 5px;'>" + ese.SubjectName + "</th>";
                }
                if (subjEdu.Count >= 0)
                {
                    header += "<th width='" + w + "px' style='text-align:right;padding-right: 5px;'>总分</th>";
                }
            }

        }
        else
        {
            if (subjSchool != null)
            {
                w = (subjSchool.Count == 0 ? 0 : w / (subjSchool.Count + 1));
                foreach (ExamSubjecySchool2 ese in subjSchool)
                {
                    header += "<th width='" + w + "' style='text-align:right;padding-right: 5px;'>" + ese.SubjectName + "</th>";
                }
                if (subjSchool.Count >= 0)
                {
                    header += "<th width='" + w + "px' style='text-align:right;padding-right: 5px;'>总分</th>";
                }
            }
        }
        return header;
    }
    protected void btnBind_Click(object sender, EventArgs e)
    {
        ////List<Classes> list = (List<Classes>)Container<IClassesLogic>.Instance.FindAll();
        ////List<Classes> newList = ConvertHtemlCode.ConvertClassHtmlCode(list);
        //List<MyTest> mytest = new List<MyTest>();
        //mytest.Add(new MyTest(1,"A3B56" , "刘德华"));
        //mytest.Add(new MyTest(2,"7C098" ,  "张学友"));
        //mytest.Add(new MyTest(3, "AB776" , "国护城"));
        //Repeater1.DataSource = mytest;
        //Repeater1.DataBind();


        examType = Convert.ToInt32(Textiseexam.Text);
        examid = Convert.ToInt32(Textid.Text);
        grade = Convert.ToInt32(Textg.Text);
        examName = TextName.Text;
        classType = Convert.ToInt32(TextClassType.Text);
        Label1.Text = examName;
        Hashtable table = null;
        if (examType == 1)
        {
            //统考
            subjEdu = Container<Meqs.Logic.Interface.IExamSubjecyEducationLogic>.Instance.GetExamCause(examid, classType);
            table = new Hashtable();
            table.Add("TableName", CommonScore.GetGradeStoreTable(grade));
            table.Add("SchoolMark", CommonScore.GetSchoolMark);
            table.Add("ExamID", examid);
            table.Add("StudentID", CommonScore.LogonStudent.RID);
            gse = Container<Meqs.Logic.Interface.IGradeScoreEducationLogic>.Instance.FindAll(table);
        }
        else
        { 
            //校考
            subjSchool = Container<Meqs.Logic.Interface.IExamSubjecySchoolLogic>.Instance.GetExamCause(examid, classType, CommonScore.GetSchoolMark);
            table = new Hashtable();
            table.Add("SchoolMark", CommonScore.GetSchoolMark);
            table.Add("ExamID", examid);
            table.Add("StudentID", CommonScore.LogonStudent.RID);
            eds = Container<Meqs.Logic.Interface.IExamDetailSchoolLogic>.Instance.FindAll(table);
        }
        List<StudentInfo> mytest = new List<StudentInfo>();
        mytest.Add(CommonScore.LogonStudent);
        Repeater1.DataSource = mytest;
        Repeater1.DataBind();
    }
    protected class StuExam
    {
        public StuExam(int t, int i, string n, int y, int g, int ct)
        {
            examType = t;
            examID = i;
            examName = n;
            schoolYear = y;
            grade = g;
            classType = ct;
        }
        public int examType;
        public int examID;
        public string examName;
        public int schoolYear;
        public int grade;
        public int classType;
    }
}
